﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateDesktopDocHtmlContent.NetIPChange
{
    public partial class NetIPChangeForm : Form
    {
        public NetIPChangeForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2_Click(false,null);
            var v1 = this.textBox1.Text;
            var v2 = this.textBox2.Text;
            var fs = new DirectoryInfo(this.textBox3.Text).GetFiles("*", SearchOption.AllDirectories);
            var okCount = 0;
            for (var i = 0; i < fs.Length; i++)
            {
                try
                {
                    var lines = File.ReadAllText(fs[i].FullName, Encoding.Default);
                    lines = Regex.Replace(lines, v1, v2);
                    File.WriteAllText(fs[i].FullName, lines, Encoding.Default);
                    okCount++;
                }
                catch (Exception err)
                {

                }
                
            }
            MessageBox.Show("完成" + okCount + "/" + fs.Length + "");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var v1 = this.textBox1.Text;
            var v2 = this.textBox2.Text;
            if (v1.EndsWith("/") && v2.EndsWith("/") == false)
            {
                this.textBox2.Text = this.textBox2.Text + "/";
                v2 = this.textBox2.Text;
            }
            if (v1.EndsWith("/") == false && v2.EndsWith("/"))
            {
                this.textBox1.Text = this.textBox1.Text + "/";
                v1 = this.textBox1.Text;
            }
            if (sender!=null&&(sender is bool)) return;
            MessageBox.Show("完成");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var folder = new SaveFileDialog();
            folder.Title = "选择一个文件表示目录";
            if (folder.ShowDialog() == DialogResult.OK)
            {
                this.textBox3.Text=Path.GetDirectoryName(folder.FileName);

                var fs = new DirectoryInfo(this.textBox3.Text).GetFiles("*", SearchOption.AllDirectories);

                this.listBox1.Items.Clear();
                for(var i = 0; i < fs.Length; i++)
                {
                    this.listBox1.Items.Add(fs[i].FullName);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem == null) return;
            var fs = File.ReadAllLines(this.listBox1.SelectedItem as string);

            var v1 = this.textBox1.Text;
            var v2 = this.textBox2.Text;


            this.textBox4.Text = "";
            for (var i=0;i<fs.Length;i++)
            {
                var t = fs[i].IndexOf("http://");
                if (t == -1) continue;
                fs[i] = fs[i].Substring(t);
                if(string.IsNullOrEmpty(v1) || string.IsNullOrEmpty(v2))
                {
                    this.textBox4.Text+=(fs[i] + "\r\n");
                    continue;
                }
                var newline = Regex.Replace(fs[i], v1, v2);
                if (newline != fs[i])
                {
                    this.textBox4.Text += (fs[i]+"--->"+ newline+"\r\n");
                }
                else
                {
                    this.textBox4.Text += (fs[i] + "\r\n");
                }
            }
           
        }
    }
}